package com.xcx.remote;

import com.xcx.common.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class WxUserRemoteService {

    /**
     * 微信用户授权登陆
     *
     * @param appid
     * @param secret
     * @param userWxCode
     * @return
     * @throws Exception
     */
    public String userAuthorizeLogin(String appid, String secret, String userWxCode) throws Exception {
        //微信授权url，测试生产一致
        String wxLoginUrl = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("appid", appid);
        parameterMap.put("secret", secret);
        parameterMap.put("js_code", userWxCode);
        parameterMap.put("grant_type", "authorization_code");
        StringBuffer sb = HttpUtils.URLGet(wxLoginUrl, parameterMap, 10 * 1000);
        log.info("userAuthorizeLogin resp:{}", sb);
        if (sb != null) {
            if (sb.toString().contains("openid")) {
                return sb.toString();
            }
        }
        return null;
    }
}
